// ex7_13_9.cpp
#include <iostream>
using namespace std;
const int SLEN = 30;
struct student
{
    char fullname[SLEN];
    char hobby[SLEN];
    int ooplevel;
};
// getinfo() has two arguments: a pointer to the first element of 
// an array of student structures and an int representing the
// number of elements of the array. The function solicits and
// stores data about students. It terminates input upon filling
// the array of upon encountering a blank line for the student
// name. The function returns the actual number of array elements
// filled.
int getinfo(student pa[], int n);
int getinfo(student pa[], int n)
{
    int cnt = 0;
    cout << "Enter student's name: ";
    while (cin >> pa[cnt].fullname)
    {
        cin.get();
        if (cin.fail())
            break;
        if (pa[cnt].fullname[0] == '\0')
            break;    
        cout << "Enter student's hobby: ";
        cin.getline(pa[cnt].hobby, SLEN);   
        cout << "Enter student's ooplevel: ";
        cin >> pa[cnt].ooplevel;
        //cin.get(); 
        ++cnt;
        if (cnt == n)
            break;    
        cout << "Enter student's name: ";    
    }
    return cnt;
}
// display1() takes a student structure as an argument
// and displays its contents
void display1(student st);
void display1(student st)
{
    cout << "\nstudent info: \n";
    cout << "name : " << st.fullname << "\n"
         << "hobby: " << st.hobby << "\n"
         << "ooplevel: " << st.ooplevel << "\n";
}
// display2() takes the address fo student structure as an 
// argument and displays the structure's contents
void display2(const student *ps);
void display2(const student *ps)
{
    cout << "\nstudent info: \n";
    cout << "name : " << ps->fullname << "\n"
         << "hobby: " << ps->hobby << "\n"
         << "ooplevel: " << ps->ooplevel << "\n";
    
}

// display3() takes the address of the first element of an array
// fo student structures and the number of array elements as 
// arguments adn displays the contents of the structures
void display3(const student pa[], int n);
void display3(const student pa[], int n)
{
    //int cnt = 0;
    cout << "\nall students' info: \n";
    for (int i = 0; i < n; i++)
    {
    cout <<"["<< i + 1 << "]" << "name : " << pa[i].fullname << "\n"
         << "["<< i + 1 << "]" << "hobby: " << pa[i].hobby << "\n"
         << "[" << i + 1 << "]"<< "ooplevel: " << pa[i].ooplevel << "\n\n";
    }
}

int main(void)
{
    cout << "Enter class size: ";
    int class_size;
    cin >> class_size;
    while (cin.get() != '\n')
        continue;

    student *ptr_stu = new student[class_size];
    int entered = getinfo(ptr_stu, class_size);
    for (int i = 0; i < entered; i++)
    {
        display1(ptr_stu[i]);
        display2(&ptr_stu[i]);
    }
    display3(ptr_stu, entered);
    delete [] ptr_stu;
    cout << "Done\n";
    return 0;
}
